﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.BLL.Extend;
using ZX.SYS.Library;
using ZX.DBModule;
using ZX.EHR.Library;
using ZX.Common;

namespace ZX.BLL
{
    public class BVFunctionRight : BaseBussnies
    {
        public BVFunctionRight()
        {
            this.entity = new V_FunctionRight();
        }

        /// <summary>
        /// 返回树型结构
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Condition"></param>
        /// <returns></returns>
        public override IList<T> GetEntitys<T>(string Condition)
        {
            IList<T> list = base.GetEntitys<T>(Condition);
            IList<V_FunctionRight> List = new List<V_FunctionRight>();
            foreach (var en in list)
            {
                V_FunctionRight _en = en as V_FunctionRight;
                List.Add(_en);
            }
            IList<V_FunctionRight> _list = new List<V_FunctionRight>();
            FillList(Guid.Empty, List, _list);
            IList<T> _mlist = new List<T>();
            foreach (var en in _list)
            {
                //object o = en;
                string json = en.ToJSON();
                T t = Common.Globals.JSonToObject<T>(json); //System.Converter;
                _mlist.Add(t);
                //T t = default(T);// (T)this.entity;
            }
            //return _list.To(List(); 
            return _mlist;
        }

        public override IList<BaseEntity> GetEntitysSplitPage(string Condition)
        {
            //return base.GetEntitysSplitPage(Condition);
            return GetEntitysByCondition(Condition);
        }
        public override IList<BaseEntity> GetEntitysByCondition(string Condition)
        {
            IList<V_FunctionRight> list = this.GetEntitys<V_FunctionRight>(Condition);
            IList<V_FunctionRight> _nlist = new List<V_FunctionRight>();
            FillList(Guid.Empty, list, _nlist);
            IList<BaseEntity> _list = new List<BaseEntity>();
            foreach (V_FunctionRight entity in _nlist)
            {
                _list.Add(entity);
            }
            return _list;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="List"></param>
        /// <param name="mList"></param>
        private void FillList(Guid parentId, IList<V_FunctionRight> List, IList<V_FunctionRight> mList)
        {
            foreach (var en in List.Where(p => p.ParentID == parentId))
            {
                if (en.ParentID != Guid.Empty)
                {
                    var _p = List.Where(p => p.FunctionId == en.ParentID).First();
                    en.Level = _p.Level + 1;
                    string str = "";

                    for (int i = 0; i < en.Level; i++)
                    {
                        str = str + ". ";
                    }
                    en.Name = str + en.Name;
                }
                else
                {
                    en.Level = 0;
                }
                mList.Add(en);
                FillList(en.FunctionId, List, mList);            
            }
        }
    }
}
